package com.tim.app.ui.fragment.person;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.application.library.base.BaseFragment;
import com.application.library.net.StringResponseCallback;
import com.application.library.widget.EmptyLayout;
import com.application.library.widget.loadmore.LoadMoreContainer;
import com.application.library.widget.loadmore.LoadMoreHandler;
import com.application.library.widget.loadmore.LoadMoreRecycleViewContainer;
import com.application.library.widget.recycle.HorizontalDividerItemDecoration;
import com.application.library.widget.recycle.WrapRecyclerView;
import com.google.gson.Gson;
import com.lzy.okhttputils.OkHttpUtils;
import com.tim.app.R;
import com.tim.app.server.api.API_User;
import com.tim.app.server.logic.UserManager;
import com.tim.app.server.result.IncomeDetailBean;
import com.tim.app.server.result.ResponseResult;
import com.tim.app.ui.adapter.personal.IncomeFlowerDetailAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wdy on 2017/8/22.
 */

public class IncomeJasmineFragment extends BaseFragment {
    private final String TAG = IncomeJasmineFragment.class.getSimpleName();
    private LoadMoreRecycleViewContainer load_more;
    private WrapRecyclerView wrvPersonial;
    private EmptyLayout emptyLayout;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_correctchange, container, false);
        initViews(rootView);
        initDate();
        loadDate(currentPage);
        return rootView;
    }

    private int currentPage = 1;
    private List<IncomeDetailBean.ResBean.ArraylistBean.ResultBean> result = new ArrayList<>();

    private void loadDate(int currentPage) {
        API_User.ins().incomeDetail(TAG, UserManager.ins().getUserId(), "flower", currentPage, 10, new StringResponseCallback() {
            @Override
            public boolean onStringResponse(String resultValue, int errCode, String errMsg, int id, boolean formCache) {
                Log.e("tag", "resultValue===" + resultValue);
                if (errCode == ResponseResult.ResultStatus.OK && resultValue != null) {
                    IncomeDetailBean incomeDetailBean = new Gson().fromJson(resultValue, IncomeDetailBean.class);
                    if (incomeDetailBean.getRes() == null || incomeDetailBean.getRes().getArraylist() == null || incomeDetailBean.getRes().getArraylist().getResult() == null) {
                        emptyLayout.showEmpty();
                        return false;
                    } else {
                        result.addAll(incomeDetailBean.getRes().getArraylist().getResult());
                    }
                    if (result.size() == 0) {
                        emptyLayout.showEmpty();
                    } else {
                        emptyLayout.showContent();
                    }
                    IncomeJasmineFragment.this.currentPage = incomeDetailBean.getRes().getArraylist().getCurrentPageNo();
                    if (incomeDetailBean.getRes().getArraylist().getCurrentPageNo() == incomeDetailBean.getRes().getArraylist().getTotalPageCount()) {
                        load_more.loadMoreFinish(false, false);
                    } else {

                        load_more.loadMoreFinish(false, true);
                    }
                    myInvatePersonAdapter.notifyDataSetChanged();
                } else {
                    emptyLayout.showError();
                }

                return false;
            }
        });

    }

    private void initDate() {
        load_more.useDefaultFooter(View.GONE);
        load_more.setAutoLoadMore(true);
        load_more.setLoadMoreHandler(loadMoreHandler);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        wrvPersonial.setLayoutManager(layoutManager);
        wrvPersonial.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity()).color(getResources().getColor(R.color.transparent)).size((int) getResources().getDimension(R.dimen.dimen_5)).build());
        emptyLayout.showLoading();

        myInvatePersonAdapter = new IncomeFlowerDetailAdapter(result);
        wrvPersonial.setAdapter(myInvatePersonAdapter);

    }

    private IncomeFlowerDetailAdapter myInvatePersonAdapter;

    private void initViews(View rootView) {
        load_more = (LoadMoreRecycleViewContainer) rootView.findViewById(R.id.load_more);
        wrvPersonial = (WrapRecyclerView) rootView.findViewById(R.id.wrvPersonial);
        wrvPersonial.setOverScrollMode(View.OVER_SCROLL_NEVER);
        emptyLayout = new EmptyLayout(getActivity(), load_more);
        emptyLayout.setErrorButtonClickListener(errorButtonClickListener);
        emptyLayout.setEmptyDrawable(R.drawable.ic_empty_follow);
        emptyLayout.setEmptyText("你还没有茉莉花", "");

    }

    private View.OnClickListener errorButtonClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            currentPage = 1;
            loadDate(currentPage);
        }
    };
    private LoadMoreHandler loadMoreHandler = new LoadMoreHandler() {
        @Override
        public void onLoadMore(LoadMoreContainer loadMoreContainer) {
            loadDate(currentPage + 1);
        }
    };

    @Override
    public void onDestroy() {
        OkHttpUtils.getInstance().cancelTag(TAG);
        super.onDestroy();
    }
}
